#define DEBUG
#pragma GCC optimize("-O2")
#include <cstdio>
#define getSum(f, a, b, x, y) (f[x][y]-f[a][y]-f[x][b]+f[a][b])

using namespace std;

const int maxn=3000;
const long long inf=0x7ffffffffffffff;

struct Data_Maker{
  long long Seed,a,b,c,MOD;
  long long Random() {
    c++;
    Seed=(Seed*a+b*c)%MOD;
    return Seed;
  }

  int Random_int() {
    int tmp=Random();
    if(Random()&1)tmp*=-1;
    return tmp;
  }

  void MAKE(int n, int m, int A[][maxn+1]){
    scanf("%lld%lld%lld%lld%lld",&Seed,&a,&b,&c,&MOD);
    for(int i=1;i<=n;i++)
      for(int j=1;j<=m;j++)
	A[i][j]=Random_int();
  }
};

inline long long max(long long a, long long b) {
  return a>b ? a : b;
}

int main() {
  freopen("rhombus.in", "r", stdin);
  freopen("rhombus.out", "w", stdout);

  static int a[maxn+1][maxn+1];
  static long long f[2*maxn][2*maxn];
  int n, m, h, ty;
  scanf("%d %d %d %d", &n, &m, &h, &ty);
  if (ty==0) {
    for (int i=1; i<=n; i++) {
      for (int j=1; j<=m; j++) {
	scanf("%d", &a[i][j]);
      }
    }
  } else {
    static struct Data_Maker dm;
    dm.MAKE(n, m, a);
  }

  for (int i=1; i<=n; i++) {
    for (int j=1; j<=m; j++) {
      f[i-j+m][i+j-1] = a[i][j];
    }
  }
  int len=n+m-1;
  for (int i=1; i<=len; i++) {
    for (int j=1; j<=len; j++) {
      f[i][j] += f[i][j-1]+f[i-1][j]-f[i-1][j-1];
    }
  }

  long long ans=-inf;
  int sn=n-h+1, sm=m-h+1;
  for (int i=h; i<=sn; i++) {
    for (int j=h; j<=sm; j++) {
      int u=i-j+m, v=i+j-1;
      ans = max(ans, getSum(f, u-h, v-h, u+h-1, v+h-1));
    }
  }

  printf("%lld", ans);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
